***********************************************************************************
***	Replication file for:                                                     	***
*** Braun, S. T. and Stuhler, J. (2024). The Economic Consequences of 			***
***	Being Widowed by War: A Life-Cycle Perspective.	     						***
***																				***
***	Journal of Public Economics                                                 ***
***   							                                                ***
*** Script:		_1-ghs-1-prep-life-cycle.do					   			 		***	
*** Purpose:	data preparation, life-cycle data GHS-2A						***
***																				***
***********************************************************************************

***
* Note: Time variable t counts months since 01/1900
* Observation period: t=229...1068 referring to 01/1919-12/1988 
***

*************************************************************************
*** RECORD 06 WG Wohngeschichte
*** RECORD 06 WG Residential history
*************************************************************************

* Not considered 

*************************************************************************
*** RECORD 07 WP Household structure and # of rooms
*************************************************************************

* Not considered

*************************************************************************
*** RECORD 09 AS Schooling
*************************************************************************

use "$widowsghs/Data/LV2A/AS.dta", clear

*** Beginning and end of school attendance

* Months 
gen m_anf=AAS04
gen m_ende=AAS06

* Assignment of unique months for ambiguous information
* Replace 32 by 12, 31 by 11 etc.
replace m_anf 	= (m_anf - 20) if  (m_anf >= 21 & m_anf <= 32)
replace m_ende 	= (m_ende - 20) if  (m_ende >= 21 & m_ende <= 32)

* Coding: Months since 1900
gen spell_beginn = AAS03*12+m_anf
gen spell_ende = AAS05*12+m_ende

*** School spell
* Generate dummies that indicate whether the respective spell is in the respective month (t=229...1068) [01/1919-12/1988] 
* Or in other words: whether school attendance is observed in the respective month 
* schulspell_`t' transfers the value of schulspell__`t' to all observations of the same person

forvalues t = 229/1068 {
gen schulspell__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen schulspell_`t'=max(schulspell__`t'), by(AIDN)
}

*** Information on school spell

* Type of school AAS01 / Leaving by grade AAS07 / Graduation/dropout AAS08

* Recode missings

recode AAS01 97/99 = .
recode AAS07 97/99 = .
recode AAS08 7/9 = . 

foreach x in AAS01 AAS07 AAS08{

forvalues t = 229/1068 {
gen `x'__`t'=.
replace `x'__`t'=`x' if schulspell__`t'==1
egen `x'_`t'=max(`x'__`t'), by(AIDN)
}

drop `x'__*
}

*** Create panel data set
* Keep only one episode per person

keep if AIDAS==1
drop schulspell__*
keep AIDN schulspell_* AAS01_* AAS07_* AAS08_*

* Reshape
reshape long schulspell_ AAS01_ AAS07_ AAS08_, i(AIDN) j(t)

* Rename and label variables

rename schulspell_ schulspell_t
recode schulspell_t . = 0
label var schulspell_t "Did the person go to school at time t?"

rename AAS01_ schulart_t
label var schulart_t "Type of school at time t"

rename AAS07_ klasse_t
label var klasse_t "After which grade did person leave school attended at time t?"

rename AAS08_ schulabschluss_t
label var schulabschluss_t "Left school attended at time t with a degree?"

* Sort and save

compress

sort AIDN t
tsset AIDN t

save "$widowsghs/processed/intermediate/LV2A_09_AS_Panel.dta", replace

*************************************************************************
*** RECORD 10 NJ NSDAP membership
*************************************************************************

* Not considered // not available in LVS2TA

*************************************************************************
*** RECORD 11 PJ Other membership
*************************************************************************

* Not considered // not available in LVS2TA

************************************************************************
*** RECORD 12 ND Details on RAD, Land-/Pflichtjahr
*************************************************************************

* Not considered

*************************************************************************
*** Record 13 AB Angaben zur Berufsausbildung
*** Record 13 AB Information on vocational training
*************************************************************************

use "$widowsghs/Data/LV2A/AB.dta", clear

*** Number of trainig episodes
egen nr_ausbildung=max(AIDAB), by(AIDN)
label var nr_ausbildung "Anzahl der Ausbildungsepisoden"
numlabel, add

*** Beginning and end of spell

* Creating variable for month of spell start and end 
table AAB03
table AAB05

gen m_anf=AAB03
gen m_ende=AAB05

* Assignment of unique months for ambiguous information
* Replace 32 by 12, 31 by 11 etc.
replace m_anf 	= (m_anf - 20) if  (m_anf >= 21 & m_anf <= 32)
replace m_ende 	= (m_ende - 20) if  (m_ende >= 21 & m_ende <= 32)

* Coding: Months since 1900
gen spell_beginn = AAB02*12+m_anf
gen spell_ende = AAB04*12+m_ende

*** Creating dummies that indicate whether the respective spell is in the respective month
forvalues t = 229/1068 {
gen a_`t'=0
replace a_`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen a`t'=max(a_`t'), by(AIDN)
}

*** Details on spell
* Ausbildungsart AAB01 // Ausbildungsabschluss AAB11 

* Recode missings
recode AAB01 8/9 = .
recode AAB11 97/99 = .

foreach x in AAB01 AAB11 {

forvalues t = 229/1068 {
gen `x'__`t'=.
replace `x'__`t'=`x' if a_`t'==1
egen `x'_`t'=max(`x'__`t'), by(AIDN)
}
drop `x'__*

}

drop a_*

*** Erzeuge Panel Datensatz
keep if AIDAB==1
keep AIDN a*  AAB01_* AAB11_* 

reshape long a AAB01_ AAB11_, i(AIDN) j(t)

* Rename and label variables
rename a ausbildungszeit_t 
label var ausbildungszeit_t "Person in training at time t?"

rename AAB01_ ausbildungsart_t 
label var ausbildungsart_t "Type of training at time t (ISCO)"

rename AAB11_ ausbildungsabschluss_t 
label var ausbildungsabschluss_t "Completion of training active at time t?"

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and save

compress

sort AIDN t

tsset AIDN t


compress
save "$widowsghs/processed/intermediate/LV2A_13_AB_PANEL.dta", replace


*************************************************************************
*** RECORD 14 Lücken zwischen Schule/Ausbildung und 1. Erwerbstätigkeit
*** RECORD 14 Gaps between school/training and 1st job  
*************************************************************************

use "$widowsghs/Data/LV2A/AL.dta", clear

*** Beginning and end of spell 

* Creating variable for month of spell start and end

gen m_anf=AAL02 
gen m_ende=AAL04

* Assignment of unique months for ambiguous information 
* Replace 32 by 12, 31 by 11 etc.
replace m_anf 	= (m_anf - 20) if  (m_anf >= 21 & m_anf <= 32)
replace m_ende 	= (m_ende - 20) if  (m_ende >= 21 & m_ende <= 32)

* Coding: Months since 1900
gen spell_beginn = AAL01*12+m_anf
gen spell_ende = AAL03*12+m_ende

*** Creating dummies that indicate whether the respective spell is in the respective month
forvalues t = 229/1068 {
gen lse_`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen lse`t'=max(lse_`t'), by(AIDN)
}

*** Gap activity
forvalues t = 229/1068 {
gen AAL05__`t'=.
replace AAL05__`t'=AAL05 if lse_`t'==1
egen AAL05_`t'=max(AAL05__`t'), by(AIDN)
replace AAL05_`t'=. if AAL05_`t'==97 | AAL05_`t'==98 | AAL05_`t'==99
}
drop AAL05__*

drop lse_*

*** Create panel data set
keep if AIDAL==1
keep AIDN lse* AAL05_*

reshape long lse AAL05_, i(AIDN) j(t)

* Rename and label

rename lse luecke_schule_erwerbs_t 
label var luecke_schule_erwerbs_t "At the time t gap between schooling and employment?"

rename AAL05_ luecke_se_aktivitität_t 
label var luecke_se_aktivitität_t "Activity in the gap between school and employment"

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and save

compress

sort AIDN t

tsset AIDN t


save "$widowsghs/processed/intermediate/LV2A_14_AL_PANEL.dta", replace



*************************************************************************
*** RECORD 15 Angaben zum Erwerbsverlauf
*************************************************************************
use "$widowsghs/Data/LV2A/BG.dta", clear

*** Number of employment spells
egen nr_jobs=max(AIDBG), by(AIDN)
label var nr_jobs "Number of different employment spells during the observation period"
numlabel, add

*** Recoding occupational prestige variable
* Code prestige as missing if occupation is missing, still in training or retired/pensioner
replace ABG02 = . if (ABG01 == 8 | ABG01 == 9 | ABG01 == 5 | ABG01 == 7) 
replace ABG27 = . if (ABG01 == 8 | ABG01 == 9 | ABG01 == 5 | ABG01 == 7) 
* Code prestige as 0 if jobseeker or non-employed housewife
replace ABG02 = 0 if ABG01 == 3 | ABG01 == 6
replace ABG27 = 0 if ABG01 == 3 | ABG01 == 6
* Replace prestige value of 0 with missing if profession is indeterminable or inadequately described
recode ABG02 0 = . if ABG01 == 4
recode ABG27 0 = . if ABG01 == 4


*** Creating dummies that indicate whether the respective spell is in the respective month
* Creating a variable for the start and end month of the employment spell
table ABG06
table ABG08

gen m_anf=ABG06 
gen m_ende=ABG08

* Assignment of unique months for ambiguous information 
* Replace 32 by 12, 31 by 11 etc.
replace m_anf 	= (m_anf - 20) if  (m_anf >= 21 & m_anf <= 32)
replace m_ende 	= (m_ende - 20) if  (m_ende >= 21 & m_ende <= 32)

* Coding: Months since 1900
gen spell_beginn = ABG05*12+m_anf
gen spell_ende = ABG07*12+m_ende

forvalues t = 229/1068 {
gen w_`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen w`t'=max(w_`t'), by(AIDN)
}

*** Spell details
* Spell No / Occupation ISCO ABG01 / Occupation prestige ABG02 / Industry of job ABG11 / Economic sector ABG12 / Occupational status ABG13 / Working hours ABG15 / Reasons for the change ABG24, ABG25

* Missings
recode ABG01 8/9 = .
recode ABG11 96/99 = .
recode ABG12 96/99 = .
recode ABG13 96/99 = .
recode ABG24 96/99 = .
recode ABG25 96/99 = .

foreach x in AIDBG ABG01 ABG02 ABG11 ABG12 ABG13 ABG15 ABG24 ABG25{

forvalues t = 229/1068 {
gen `x'__`t'=.
replace `x'__`t'=`x' if w_`t'==1
egen `x'_`t'=max(`x'__`t'), by(AIDN)
}

drop `x'__*

}

drop w_*


*** Create panel data set

* Keep only one episode per person
keep if AIDBG==1
keep AIDN w* AIDBG_* ABG01_* ABG02_* ABG11_* ABG13_* ABG15_* 

* Reshape
reshape long w AIDBG_ ABG01_ ABG02_ ABG11_ ABG13_ ABG15_, i(AIDN) j(t)

* Rename and label variables
rename w erwerbstaetig_t
label var erwerbstaetig_t "Employed at time t?"

rename AIDBG_ spell_no_t
label var spell_no_t "Spell number at time t?"

rename ABG01_ beruf_t
label var beruf_t "ABG01: Occupation at time t (ISCO)"

rename ABG02_ beruf_prestige_t
label var beruf_prestige_t "ABG02: Occupational prestige at time t (Treiman)"

rename ABG11_ branche_t
label var branche_t "ABG11: Economic sector at time t"

rename ABG13_ beruf_stellung_t
label var beruf_stellung_t "ABG13: Occupational status at time t"

rename ABG15_ wochenstunden_t
label var wochenstunden_t "ABG15: Weekly working at time t"

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and save

compress

sort AIDN t

tsset AIDN t

save "$widowsghs/processed/intermediate/LV2A_15_BG_PANEL.dta", replace


*************************************************************************
*** Record 16 BL Erwerbslücken 
*** Record 16 BL Gaps in employment history 
*************************************************************************

use "$widowsghs/Data/LV2A/BL.dta", clear

*** Beginning and end of spell

* Generate a variable for the start and end month of the employment gap 

gen m_anf=ABL03 
gen m_ende=ABL05

* Assignment of unique months for ambiguous information
* Replace 32 by 12, 31 by 11 etc.
replace m_anf 	= (m_anf - 20) if  (m_anf >= 21 & m_anf <= 32)
replace m_ende 	= (m_ende - 20) if  (m_ende >= 21 & m_ende <= 32)


* Coding: Months since 1900
gen spell_beginn = ABL02*12+m_anf
gen spell_ende = ABL04*12+m_ende

* Indicator for gap at time t
forvalues t = 229/1068 {
gen l_`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen l`t'=max(l_`t'), by(AIDN)
}


*** Details on gap activity

* Missings
recode ABL01 96/99 = .
recode ABL07 96/99 = .
recode ABL08 96/99 = .

* Gap activity ABL01 / Reasons for change ABL07/08

foreach x in `x' ABL01 ABL07 ABL08{

forvalues t = 229/1068 {
gen `x'__`t'=.
replace `x'__`t'=`x' if l_`t'==1
egen `x'_`t'=max(`x'__`t'), by(AIDN)
}
drop `x'__*

}
drop l_*

*** Create panel data set

keep if AIDBL==1

sort AIDN
keep AIDN l* ABL01_* ABL07_* ABL08_*

reshape long l ABL01_ ABL07_ ABL08_, i(AIDN) j(t)

* Rename and label variables

rename l erwerbsluecke_t 
recode erwerbsluecke_t . = 0
label var erwerbsluecke_t "Gap in employment at time t?"

rename ABL01_ erwerbsluecke_taetigkeit_t 
label var erwerbsluecke_taetigkeit_t "ABL01: Activity during employment gap at time t"

rename ABL07_ grund1_erwerbsluecke_t 
label var grund1_erwerbsluecke_t "ABL07: Reason 1 for employment gap at time t"

rename ABL08_ grund2_erwerbsluecke_t 
label var grund2_erwerbsluecke_t "ABL08: Reason 2 for employment gap at time t"

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and Save

compress

sort AIDN t 
tsset AIDN t

save "$widowsghs/processed/intermediate/LV2A_16_BL_PANEL.dta", replace

*************************************************************************
*** RECORD 17 BN Nebentaetigkeiten
*************************************************************************

* Not considered

*************************************************************************
*** RECORD 18 NA Arbeitseinsaetze
*************************************************************************

* Not considered

*************************************************************************
*** RECORD 19 BW Angaben zur beruflichen Weiterbildung
*************************************************************************

* Not considered

*************************************************************************
*** RECORD 21 KR Angaben zur Krankengeschichte
*** RECORD 21 KR Information on medical history
*************************************************************************

use "$widowsghs/Data/LV2A/PA.dta", clear

*** Construct birth and interview date in month since 1900

rename APA02 geburtsjahr
label var geburtsjahr "APA02: Year of birth"
recode geburtsjahr 97/99 = .

rename APA03 geburtsmonat
label var geburtsmonat "APA03: Month of birth"
recode geburtsmonat 97/99 = . 

gen geburtsdatum_monate = geburtsjahr*12+geburtsmonat
label var geburtsdatum_monate "Date of birth in months since 1900"

gen time_interview = APA39*12+APA40

keep AIDN geburtsdatum_monate time_interview

merge 1:m AIDN using "$widowsghs/Data/LV2A/KR.DTA"
keep if _merge == 3
drop _merge

*** Create a variable for the start and end of the illness

* Acute illness

* No info on month, so assume spell begins in June
gen spell_beginn_akut = 12 * AKR07 + 6
recode AKR08 996 / 999 = .
* Assumption: lasts for two months (median of duration) if missing
gen spell_ende_akut = spell_beginn_akut + round(AKR08 / 4)
replace spell_ende_akut = spell_beginn_akut + 6 if (spell_ende_akut == . & spell_beginn_akut != .)

* Chronic illness
gen spell_beginn_chronisch = 12 * AKR09 + 6
replace spell_beginn_chronisch = geburtsdatum_monate if  AKR09 == 96

gen spell_ende_chronisch = 12 * AKR10 + 6
replace spell_ende_chronisch = time_interview if AKR10 == 96 & spell_beginn_chronisch != .

*** Generate indicators for illness at time t

* Acute

forvalues t = 229/1068 {
gen illness_acute__`t'=1 if spell_beginn_akut <= `t' & spell_ende_akut >= `t'
egen illness_acute_`t'=max(illness_acute__`t'), by(AIDN)
}

* Chronic

forvalues t = 229/1068 {
gen illness_chronic__`t'=1 if spell_beginn_chronisch <= `t' & spell_ende_chronisch >= `t'
egen illness_chronic_`t'=max(illness_chronic__`t'), by(AIDN)
}

*** Characteristics of illness

* Body part 1/2 AKR01 AKR03 // Function 1/2 AKR02 AKR04 

* Missings
recode AKR01 99 9997/9999 = .
recode AKR02 99 9997/9999 = .
recode AKR03 99 9997/9999 = .
recode AKR04 99 9997/9999 = .


foreach x in AKR01 AKR03 AKR02 AKR04 {

forvalues t = 229/1068 {
gen `x'_acute__`t'=.
replace `x'_acute__`t'=`x' if illness_acute__`t'==1
egen `x'_acute_`t'=max(`x'_acute__`t'), by(AIDN)
}
drop `x'_acute__*

}

foreach x in AKR01 AKR03 AKR02 AKR04 {

forvalues t = 229/1068 {
gen `x'_chronic__`t'=.
replace `x'_chronic__`t'=`x' if illness_chronic__`t'==1
egen `x'_chronic_`t'=max(`x'_chronic__`t'), by(AIDN)
}
drop `x'_chronic__*

}

drop illness_chronic__* illness_acute__* 

*** Create panel data set

keep if AIDKR==1

sort AIDN
keep AIDN illness_chronic_* illness_acute_* AKR01_chronic_* AKR03_chronic_* AKR02_chronic_* AKR04_chronic_* AKR01_acute_* AKR03_acute_* AKR02_acute_* AKR04_acute_*

reshape long illness_chronic_ AKR01_chronic_ AKR03_chronic_ AKR02_chronic_ AKR04_chronic_ illness_acute_ AKR01_acute_ AKR03_acute_ AKR02_acute_ AKR04_acute_, i(AIDN) j(t)

* Rename and label variables

rename illness_chronic_ illness_chronic_t 
label var illness_chronic_t "Chronic illness at time t?"

rename AKR01_chronic_ leiden_koeper1_chronic_t
label var leiden_koeper1_chronic_t "AKR01: Type of suffering (body) during illness at time t, mention 1"

rename AKR03_chronic_ leiden_koeper2_chronic_t
label var leiden_koeper2_chronic_t "AKR03: Type of illness (body) during illness at time t, mention 2"

rename AKR02_chronic_ leiden_funkt1_chronic_t
label var leiden_funkt1_chronic_t "AKR02: Type of illness (function) during illness at time t, mention 1"

rename AKR04_chronic_ leiden_funkt2_chronic_t
label var leiden_funkt2_chronic_t "AKR04: Type of illness (function) during illness at time t, mention 2"


rename illness_acute_ illness_acute_t 
label var illness_acute_t "Acute illness at time t?"

rename AKR01_acute_ leiden_koeper1_acute_t
label var leiden_koeper1_acute_t "AKR01: Type of suffering (body) during illness at time t, mention 1"

rename AKR03_acute_ leiden_koeper2_acute_t
label var leiden_koeper2_acute_t "AKR03: Type of illness (body) during illness at time t, mention 2"

rename AKR02_acute_ leiden_funkt1_acute_t
label var leiden_funkt1_acute_t "AKR02: Type of illness (function) during illness at time t, mention 1"

rename AKR04_acute_ leiden_funkt2_acute_t
label var leiden_funkt2_acute_t "AKR04: Type of illness (function) during illness at time t, mention 2"


label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and Save

compress

sort AIDN t 
tsset AIDN t

save "$widowsghs/processed/intermediate/LV2A_21_KR_PANEL.dta", replace



*************************************************************************
*** RECORD 23 Angaben zu Ehe/Partnerschaft
*** RECORD 23 Details of marriage/partnership
*************************************************************************

use "$widowsghs/Data/LV2A/FP.dta", clear

*** AIDN==20222 & AIDFP==1 ohne Angabe des Jahres 
drop if AIDN==20222


*** Total number of marriages
egen anzahl_ehen=max(AIDFP), by(AIDN)
label var anzahl_ehen "Anzahl der Ehen"


*** Generate indicator for marital status at time t

*** Spell beginning and end
* Coding: Months since 1900

* Year of separation (due to divorce or death)
gen trennung=.
replace trennung=AFP03 if AFP03 !=.t
replace trennung=AFP05 if AFP05 !=.t
replace trennung=45 if AFP05 == 95 /* Code "missing in action" as 1945 */

* All missing months are replaced by June
recode AFP02 96/99 = 6
recode AFP02 . = 6

* By assumption, the separation always takes place in June (as no month is specified)
* If no year of separation specified => still together by assumption

gen m_anf=AFP02
gen spell_beginn = AFP01*12+m_anf
gen m_ende=6
gen spell_ende = trennung*12+m_ende

*** Indicator for whether person was married at time t
forvalues t = 229/1068 {
gen ehe_`t'=0
replace ehe_`t'=1 if spell_beginn <= `t' & (spell_ende >=`t' | spell_ende == .)
}

* Value is transferred to all episodes of the person
forvalues t = 229/1068 {
egen ehe`t'=max(ehe_`t'), by(AIDN)
}


*** Characteristics of spell

* Missings

recode AFP05 97/99 = .
recode AFP07 9997/9999 = .
recode AFP08 8/9 = .
recode AFP13 97/99 = .


* How many marriages? AIDFP // Partner's year of death (95 = missing in action) AFP05 // Partner's year of birth AFP07 // // Partner's school-leaving qualification AFP08 // Partner's occupation AFP09 // Partner's occupation AFP10 // Partner's vocational training AFP13

foreach x in AIDFP AFP05 AFP07 AFP08 AFP09 AFP10 AFP13 {

forvalues t = 229/1068 {
gen `x'__`t'=`x' if ehe_`t'==1
egen `x'_`t'=max(`x'__`t'), by(AIDN)
}
drop `x'__*
}

* Reason for separation: Divorce=1, Death=2
forvalues t = 229/1068 {
gen tr_grund__`t'=.
replace tr_grund__`t'=1 if ehe_`t'==1 & AFP03~=.t
replace tr_grund__`t'=2 if ehe_`t'==1 & AFP05~=.t
egen tr_grund_`t'=max(tr_grund__`t'), by(AIDN)
}
drop tr_grund__*

drop ehe_*


*** Create panel data set
keep if AIDFP==1
keep AIDN ehe* AIDFP_*  AFP05_* AFP07_* AFP08_* AFP09_* AFP10_* AFP13_* tr_grund_*

reshape long ehe AIDFP_ AFP05_ AFP07_ AFP08_ AFP09_ AFP10_ AFP13_ tr_grund_, i(AIDN) j(t)

* Rename and label variables

rename ehe verheiratet_t 
label var verheiratet_t "Married at time t?"

rename AIDFP_ ehenr_t 
label var ehenr_t "Marriage at time t is the how manyth marriage of a person?"

rename AFP05_ todesjahr_ehepartner_t 
label var todesjahr_ehepartner_t "AFP05: Year of death of the respective spouse"

rename AFP07_ geburtsjahr_ehepartner_t 
label var geburtsjahr_ehepartner_t "AFP07: Year of birth of the respective spouse"

rename AFP08_ schulabschluss_ehepartner_t 
label var schulabschluss_ehepartner_t "AFP08: School-leaving certificate of the respective spouse"

rename AFP09_ beruf_ehepartner_t 
label var beruf_ehepartner_t "AFP09: Profession learned by the respective spouse"

rename AFP10_ beruf_prest_ehepartner_t 
label var beruf_prest_ehepartner_t "AFP10: Prestige of the profession learned by the respective spouse"

rename AFP13_ ausbildung_ehepartner_t 
label var ausbildung_ehepartner_t "AFP13: Vocational training of the respective spouse"

rename tr_grund_ trennungs_grund_t 
label var trennungs_grund_t "AFP13: Reason for separation (1=divorce, 2=death)"

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

* Order and save

compress

sort AIDN t 
tsset AIDN t

save "$widowsghs/processed/intermediate/LV2A_23_FP_PANEL.dta", replace



*************************************************************************
*** RECORD 24 FT Erwerbstätigkeit Partner
*************************************************************************

* Not considered 

*************************************************************************
*** RECORD 25 FK Kinder
*** RECORD 25 FK Children
*************************************************************************

* Convert first into cross-section; panel-data when merged with other files

use "$widowsghs/Data/LV2A/FK.dta", clear

*** Recoding birth time in months 

*** all missing months are replaced by June
recode AFK03 96/99 = 6
recode AFK03 . = 6
recode AFK03 26 = 6

gen geburt_monaten = AFK02*12 + AFK03

keep AIDN AIDFK geburt_monaten

* Create cross section

reshape wide geburt_monaten, i(AIDN) j(AIDFK)

save "$widowsghs/processed/intermediate/LV2A_25_FK_PANEL.dta", replace


**************************************************
* RECORD PA Querschnittsdaten zur Indexperson
* RECORD PA Cross-sectional data for index person
**************************************************

use "$widowsghs/Data/LV2A/PA.dta", clear

rename APA01 geschlecht
label var geschlecht "APA01: Gender (1=male, 2=female)"
recode geschlecht 7/9 = .

rename APA02 geburtsjahr
*replace geburtsjahr = geburtsjahr + 1900
label var geburtsjahr "APA02: Year of birth"
recode geburtsjahr 97/99 = .

rename APA03 geburtsmonat
label var geburtsmonat "APA03: Month of birth"
recode geburtsmonat 97/99 = . 

gen geburtsdatum_monate = geburtsjahr*12+geburtsmonat
label var geburtsdatum_monate "Date of birth in months since 1900"

gen index = 2001
label var index "Generation index"

keep AIDN geburt* geschlecht index

save "$widowsghs/processed/intermediate/LV2A_Querschnitt_IP_Panel.dta", replace


*************************************************************************
*** Creating life-cycle data set
*************************************************************************


use "$widowsghs/processed/intermediate/LV2A_09_AS_Panel.dta"

merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_13_AB_PANEL.dta"
drop _merge 
merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_14_AL_PANEL.dta"
drop _merge 
merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_15_BG_PANEL.dta"
drop _merge 
merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_16_BL_PANEL.dta"
drop _merge 
merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_21_KR_PANEL.dta"
drop _merge 
merge m:m AIDN t using "$widowsghs/processed/intermediate/LV2A_23_FP_PANEL.dta"
drop _merge 


merge m:m AIDN using "$widowsghs/processed/intermediate/LV2A_Querschnitt_IP_Panel.dta"
drop _merge 

label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

gen jahr = 1900 + floor((t-1)/12)
label var jahr "Year"

gen monat = t - (jahr-1900)*12
label var monat "Month"

gen alter_t = t - geburtsdatum_monate
replace alter_t = . if alter_t < 0 
label var alter_t "Age at time t in months"

gen alter_j = jahr - (geburtsjahr+1900)
replace alter_j = . if alter_j < 0  
label var alter_j "Age in year j in years"

* Anzahl Kinder über Zeit

merge m:1 AIDN using "$widowsghs/processed/intermediate/LV2A_25_FK_PANEL.dta"
drop _merge

gen 	anzahl_kinder_t = 0
replace anzahl_kinder_t = 1 if (geburt_monaten1 <= t & geburt_monaten1 != .)
replace anzahl_kinder_t = 2 if (geburt_monaten2 <= t & geburt_monaten2 != .)
replace anzahl_kinder_t = 3 if (geburt_monaten3 <= t & geburt_monaten3 != .)
replace anzahl_kinder_t = 4 if (geburt_monaten4 <= t & geburt_monaten4 != .)
replace anzahl_kinder_t = 5 if (geburt_monaten5 <= t & geburt_monaten5 != .)
replace anzahl_kinder_t = 6 if (geburt_monaten6 <= t & geburt_monaten6 != .)
replace anzahl_kinder_t = 7 if (geburt_monaten7 <= t & geburt_monaten7 != .)
replace anzahl_kinder_t = 8 if (geburt_monaten8 <= t & geburt_monaten8 != .)

drop geburt_monate*

compress




rename AIDN fid
label var fid "Family ID"

gen LV2A = 1

compress

sort fid t

tsset fid t

save "$widowsghs/processed/intermediate/LV2A_PANELDATEN_Index.dta", replace




*** Order variables 

use "$widowsghs/processed/intermediate/LV2A_PANELDATEN_Index.dta", clear

* Panel IDs

gen ___Panel_Data______________ = .
gen ___Panel_IDs________________ = .

order ___Panel_Data______________ ___Panel_IDs________________ fid t monat jahr, first

* Schooling

gen ___Schooling_______________ = .
order ___Schooling_______________ schulspell_t schulart_t klasse_t schulabschluss_t, after(jahr)

* Education, vocational training 

gen ___Voc_Training___________ = .
order ___Voc_Training___________ ausbildungszeit_t ausbildungsart_t ausbildungsabschluss_t, after(schulabschluss_t)

 
* Employment

gen ___Employment___________ = .
order ___Employment___________ erwerbstaetig_t spell_no_t beruf_t beruf_prestige_t branche_t beruf_stellung_t wochenstunden_t, after(ausbildungsabschluss_t)

* Interruption of education and working life

gen ___Interruptions________ = .
order ___Interruptions________ luecke_schule_erwerbs_t luecke_se_aktivitität_t erwerbsluecke_t erwerbsluecke_taetigkeit_t grund1_erwerbsluecke_t grund2_erwerbsluecke_t, after(wochenstunden_t)  

* Illnesses

drop leiden_koeper1_acute_t leiden_koeper2_acute_t leiden_funkt1_acute_t leiden_funkt2_acute_t leiden_funkt1_chronic_t leiden_funkt2_chronic_t leiden_koeper1_chronic_t leiden_koeper2_chronic_t leiden_funkt1_chronic_t leiden_funkt2_chronic_t
gen ___Illness________________ = .
order ___Illness________________ illness_acute_t illness_chronic_t, after(grund2_erwerbsluecke_t)


* Marriage

gen ___Marriage________________ = .
order ___Marriage________________ verheiratet_t ehenr_t todesjahr_ehepartner_t geburtsjahr_ehepartner_t schulabschluss_ehepartner_t beruf_ehepartner_t beruf_prest_ehepartner_t ausbildung_ehepartner_t trennungs_grund_t, after(illness_chronic_t) 

* Age 

gen ___Age___________________ = .
order ___Age___________________ alter_t alter_j, after(trennungs_grund_t)

* Family

gen ___Family_________________ = .
order ___Family_________________ anzahl_kinder_t, after(alter_j)

* Lösche Cross-sectional data 

drop geschlecht geburtsjahr geburtsmonat geburtsdatum_monate index LV2A

compress

save "$widowsghs/processed/intermediate/LV2A_PANELDATEN_Index_edt.dta", replace



